<?php

namespace ESign\SignFlow;

use ESign\Core\AbstractAPI;
use ESign\Exceptions\HttpException;
use ESign\Support\Collection;

class SignFlow extends AbstractAPI
{
    const NOTICE_TYPE_SMS = '1';
    const NOTICE_TYPE_EMAIL = '2';
    const NOTICE_TYPE_NULL = '';
    public function createFlowOneStep($params){
        $url = '/api/v2/signflows/createFlowOneStep';
        return $this->parseJSON('json', [$url, $params]);
    }
    /**
     * 签署流程创建
     *
     * @param $businessScene
     * @param $noticeDeveloperUrl
     * @param $autoArchive
     * @return Collection|null
     * @throws HttpException
     */
    public function createSignFlow($businessScene, $noticeDeveloperUrl = null, $autoArchive = true,$contractValidity=0,$signValidity=0)
    {
        $url = '/v1/signflows';
        $params = [
            'autoArchive'   => $autoArchive,
            'businessScene' => $businessScene,
            'configInfo' => [
                'noticeDeveloperUrl' => $noticeDeveloperUrl
            ]
        ];
        if($contractValidity>0){
            $params['contractValidity'] = $contractValidity*1000;
        }
        if($signValidity>0){
            $params['signValidity'] = $signValidity*1000;
        }
        return $this->parseJSON('json', [$url, $params]);
    }

    /**
     * 流程文档添加
     *
     * @param $flowId
     * @param $fileId
     * @param int $encryption
     * @param null $fileName
     * @param null $filePassword
     * @return Collection|null
     * @throws HttpException
     */
    public function addDocuments($flowId, $fileId, $encryption = 0, $fileName = null, $filePassword = null)
    {
        $url = "/v1/signflows/{$flowId}/documents";
        $params = [
            'docs' => [
                ['fileId' => $fileId, 'encryption' => $encryption, 'fileName' => $fileName, 'filePassword' => $filePassword],
            ]
        ];
        return $this->parseJSON('json', [$url, $params]);
    }
    /**
     * 获取流程文档
     *
     * @param $flowId
     * @param $fileId
     * @param int $encryption
     * @param null $fileName
     * @param null $filePassword
     * @return Collection|null
     * @throws HttpException
     */
    public function getDocuments($flowId)
    {
        $url = "/v1/signflows/{$flowId}/documents";
        $params = [
        ];
        return $this->parseJSON('get', [$url, $params]);
    }
    /**
     * 添加平台自动盖章签署区
     *
     * @param $flowId
     * @param $fileId
     * @param $sealId
     * @param $posPage
     * @param $posX
     * @param $posY
     * @param $signDateBeanType
     * @param $signDateBean
     * @param $signType
     * @return Collection|null
     * @throws HttpException
     */
    public function addPlatformSign($flowId, $fileId, $sealId, $posBean, $signDateBeanType = 0, $signDateBean = null, $signType = null,$thirdOrderNo=null)
    {
        $url = "/v1/signflows/{$flowId}/signfields/platformSign";
        $signFieldOne = [
            'fileId' => $fileId,
            'sealId' => $sealId,
            'posBean' => $posBean,
            'signDateBeanType' => $signDateBeanType,
            'signDateBean' => $signDateBean,
            'signType' => $signType,
            'thirdOrderNo'  =>  $thirdOrderNo
        ];

        $params = [
            'signfields' => [
                $signFieldOne,
            ]
        ];
        return $this->parseJSON('json', [$url, $params]);
    }

    /**
     * 添加签署方自动盖章签署区
     *
     * @param $flowId
     * @param $fileId
     * @param $authorizedAccountId
     * @param $sealId
     * @param $posPage
     * @param $posX
     * @param $posY
     * @param int $signDateBeanType
     * @param null $signDateBean
     * @param null $signType
     * @return Collection|null
     * @throws HttpException
     */
    public function addAutoSign($flowId, $fileId, $authorizedAccountId, $sealId, $posPage, $posX, $posY, $signDateBeanType = 0, $signDateBean = null, $signType = null)
    {
        $url = "/v1/signflows/{$flowId}/signfields/autoSign";
        $signFieldOne = [
            'fileId' => $fileId,
            'authorizedAccountId' => $authorizedAccountId,
            'sealId' => $sealId,
            'posBean' => [
                'posPage' => $posPage,
                'posX' => $posX,
                'posY' => $posY,
            ],
            'signDateBeanType' => $signDateBeanType,
            'signDateBean' => $signDateBean,
            'signType' => $signType,
        ];

        $params = [
            'signfields' => [
                $signFieldOne,
            ]
        ];
        return $this->parseJSON('json', [$url, $params]);
    }

    /**
     * 添加手动盖章签署区
     *
     * @param $flowId
     * @param $fileId
     * @param $signerAccountId
     * @param $posPage
     * @param $posX
     * @param $posY
     * @param $signDateBeanType
     * @param $signDateBean
     * @return Collection|null
     * @throws HttpException
     */
    public function addHandSign($flowId, $fileId, $signerAccountId, $posPage=0, $posX=0, $posY=0, $signDateBeanType = 0, $signDateBean = null)
    {
        $url = "/v1/signflows/{$flowId}/signfields/handSign";
        $signFieldOne = [
            'fileId' => $fileId,
            'signerAccountId' => $signerAccountId,
            'posBean' => [
                'posPage' => $posPage,
                'posX' => $posX,
                'posY' => $posY,
            ],
            'signDateBeanType' => $signDateBeanType,
            'signDateBean' => $signDateBean,
        ];

        $params = [
            'signfields' => [
                $signFieldOne,
            ]
        ];
        return $this->parseJSON('json', [$url, $params]);
    }

    /**
     * 流程开始
     *
     * @param $flowId
     * @return Collection|null
     * @throws HttpException
     */
    public function startSignFlow($flowId)
    {
        $url = "/v1/signflows/{$flowId}/start";
        return $this->parseJSON('put', [$url]);
    }

    /**
     * 获取签署地址
     * @param $flowId
     * @param $accountId
     * @param null $orgId
     * @param int $urlType
     * @param null $appScheme
     * @return Collection|null
     * @throws HttpException
     */
    public function getExecuteUrl($flowId, $accountId, $orgId = null, $urlType = 0, $appScheme = null)
    {
        $url = "/v1/signflows/{$flowId}/executeUrl";
        $params = [
            'accountId' => $accountId,
            'organizeId' => $orgId,
            'urlType' => $urlType,
            'appScheme' => $appScheme
        ];
        return $this->parseJSON('get', [$url, $params]);
    }

    /**
     * 添加手动盖章签署区
     *
     * @param $flowId
     * @param $fileId
     * @param $signerAccountId
     * @param $posPage
     * @param $posX
     * @param $posY
     * @param $signDateBeanType
     * @param $signDateBean
     * @return Collection|null
     * @throws HttpException
     */
    public function addHandSigns($flowId,$signFields)
    {
        $url = "/v1/signflows/{$flowId}/signfields/handSign";
        $params = [
            'signfields' => $signFields
        ];
//        echo json_encode($params);exit;
        return $this->parseJSON('json', [$url, $params]);
    }

    public function signFields($fileId, $signerAccountId, $posBean, $signDateBeanType = 0, $signDateBean = null,$thirdOrderNo=null) {
        return [
            'fileId' => $fileId,
            'signerAccountId' => $signerAccountId,
            'posBean' => $posBean,
            'signDateBeanType' => $signDateBeanType,
            'signDateBean' => $signDateBean,
            'thirdOrderNo'  =>  $thirdOrderNo
        ];
    }

    /**
     * 流程撤销
     * @param $flowId
     * @param null $revokeReason
     * @param null $operatorId
     * @return Collection|null
     * @throws HttpException
     */
    public function revoke($flowId,$revokeReason=null,$operatorId=null){
        $url = "/v1/signflows/{$flowId}/revoke";
        $params = [
            'revokeReason'  => $revokeReason,
            'operatorId'    => $operatorId
        ];
        return $this->parseJSON('put', [$url, $params]);
    }

    /**
     * 获取签署链接
     * @param $flowId
     * @param $accountId
     * @return Collection|null
     * @throws HttpException
     */
    public function executeUrl($flowId,$accountId){
        $url = "/v1/signflows/{$flowId}/executeUrl";
        $params = [
            'accountId'  => $accountId,
        ];
        return $this->parseJSON('get', [$url, $params]);
    }

    /**
 * 添加附件
 * @param $flowId
 * @param $attachments
 * @return Collection|null
 * @throws HttpException
 */
    public function addAttachments($flowId,$attachments){
        $url = "/v1/signflows/{$flowId}/attachments";
        $params = [
            'attachments'  => $attachments,
        ];
        return $this->parseJSON('json', [$url, $params]);
    }
    /**
     * 添加附件
     * @param $flowId
     * @param $attachments
     * @return Collection|null
     * @throws HttpException
     */
    public function getAttachments($flowId){
        $url = "/v1/signflows/{$flowId}/attachments";
        return $this->parseJSON('get', [$url]);
    }
}